import indexObj from '../../unit/index.js';
import event from '../../unit/event';
import states from '../states';
import constVal from '../../unit/const.js';
import musicobj from '../../unit/music';
import Block from '../../unit/block.js';
let {
	want
} = indexObj;
let {
	music
} = musicobj;
let {
	speeds,
	delays
} = constVal;

const down = (store) => {
	store.commit("left", true);
	event.down({
		key: 'left',
		begin: 200,
		interval: 100,
		callback: () => {
			const state = store.state;
			if (state.lock) {
				return;
			}
			if (music.move) {
				music.move();
			}
			const cur = state.cur;
			if (cur !== null) {
				if (state.pause) {
					states.pause(false);
					return;
				}
				const next = cur.left();
				const delay = delays[state.speedRun - 1];
				let timeStamp;
				if (want(next, state.matrix)) {
					next.timeStamp += parseInt(delay, 10);
					store.commit("moveBlock", next.reset === true ? null : new Block(next));
					timeStamp = next.timeStamp;
				} else {
					cur.timeStamp += parseInt(parseInt(delay, 10) / 1.5, 10); // 真实移动delay多一点，碰壁delay少一点
					// store.dispatch(actions.moveBlock(cur));
					store.commit("moveBlock", cur.reset === true ? null : new Block(cur));
					timeStamp = cur.timeStamp;
				}
				const remain = speeds[state.speedRun - 1] - (Date.now() - timeStamp);
				states.auto(remain);
			} else {
				let speed = state.speedStart;
				speed = speed - 1 < 1 ? 6 : speed - 1;
				// store.dispatch(actions.speedStart(speed));
				store.commit("speedStart", speed);
			}
		},
	});
};

const up = (store) => {
	store.commit("left", false);
	event.up({
		key: 'left',
	});
};

export default {
	down,
	up,
};